package com.zxy.supplier_system.server.repository

import com.zxy.supplier_system.server.controller.GoodsBrandGroupController
import com.zxy.supplier_system.server.entity.GoodsBrandGroup
import com.zxy.supplier_system.server.entity.SystemService
import com.zxy.supplier_system.server.utils.SpecificationUtils
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor

interface GoodsBrandGroupRepository :JpaRepository<GoodsBrandGroup,Int>,JpaSpecificationExecutor<GoodsBrandGroup>{

    fun findAllBySystemService(systemService: SystemService):List<GoodsBrandGroup>

    fun findByIdAndSystemService(id: Int, systemService: SystemService):GoodsBrandGroup?

    fun findFirstByNameAndSystemService(name: String, systemService: SystemService):GoodsBrandGroup?

    object Specifications{

        fun fromQueryGoodsBrandGroupsRequest(queryGoodsBrandGroupsRequest: GoodsBrandGroupController.QueryGoodsBrandGroupsRequest): Specification<GoodsBrandGroup> {
            return Specification.allOf(nameContains(queryGoodsBrandGroupsRequest.name))
        }

        fun nameContains(name:String?):Specification<GoodsBrandGroup>{
            return SpecificationUtils.contains({
                it.get("name")
            },name)
        }

    }

}
